package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.BetterPlanBean;
import com.hxkcloud.patient.utils.ImageUtil;

import java.util.List;

/**
 * Created by william_sun on 2016-11-29.
 * Description:患者康复方案列表
 */
public class BetterPlanAdapter extends BaseExpandableListAdapter {

    private List<BetterPlanBean> datas;
    private Context mContext;
    private LayoutInflater mInflater;
    private onBetterplanFunctionListener listener;
    private int type;//0单子没发送 1模板有发送助手

    public void setDatas(List<BetterPlanBean> datas) {
        this.datas = datas;
        handler.sendEmptyMessage(REFRESH);
    }

    public BetterPlanAdapter(Context mContext, int type, List<BetterPlanBean> datas, onBetterplanFunctionListener listener) {
        this.mContext = mContext;
        this.type = type;
        this.datas = datas;
        this.listener = listener;
        mInflater = LayoutInflater.from(mContext);

    }

    private final int REFRESH = 222;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == REFRESH) {
                observer.onChanged();
            }
        }
    };

    private DataSetObserver observer;

    //观察者模式
    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        this.observer = observer;
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        this.observer = null;
    }


    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return datas.get(groupPosition).getOrderDetailList().size() + 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return datas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return datas.get(groupPosition).getOrderDetailList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return (groupPosition + 1) * (childPosition + 1);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        viewholderTitle vhTitle;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_noticedo_title, null);
            vhTitle = new viewholderTitle();
            vhTitle.tv_title = (TextView) convertView.findViewById(R.id.tv_noticedotitle_name);
            vhTitle.iv_arrow = (ImageView) convertView.findViewById(R.id.iv_noticedotitle_arrow);
            convertView.setTag(vhTitle);
        } else {
            vhTitle = (viewholderTitle) convertView.getTag();
        }
        vhTitle.tv_title.setText(datas.get(groupPosition).getOrderName());
        if (isExpanded) {
            vhTitle.iv_arrow.setImageResource(R.mipmap.tab_arrow_up1);
        } else {
            vhTitle.iv_arrow.setImageResource(R.mipmap.tab_arrow1);
        }
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        viewholderInfo vhInfo;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_betterplan_info, null);
            vhInfo = new viewholderInfo();
            vhInfo.ll_func = (LinearLayout) convertView.findViewById(R.id.ll_betterplan_func);
            vhInfo.bt_delete = (Button) convertView.findViewById(R.id.bt_betterplan_delete);
            vhInfo.bt_modify = (Button) convertView.findViewById(R.id.bt_betterplan_modify);
            vhInfo.bt_send = (Button) convertView.findViewById(R.id.bt_betterplan_send);
            vhInfo.ll_child = (LinearLayout) convertView.findViewById(R.id.ll_betterplan_child);
            vhInfo.view_line1 = convertView.findViewById(R.id.view_betterplan_line1);
            vhInfo.view_line2 = convertView.findViewById(R.id.view_betterplan_line2);
            vhInfo.tv_reminddate = (TextView) convertView.findViewById(R.id.tv_betterplan_reminddate);
            vhInfo.ll_items = (LinearLayout) convertView.findViewById(R.id.ll_betterplan_items);
            convertView.setTag(vhInfo);
        } else {
            vhInfo = (viewholderInfo) convertView.getTag();
        }
        if (childPosition == 0) {
            vhInfo.ll_func.setVisibility(View.VISIBLE);
            vhInfo.ll_child.setVisibility(View.GONE);
            vhInfo.bt_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.delete(groupPosition);
                }
            });
            vhInfo.bt_modify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.modify(groupPosition);
                }
            });
            if (type == 1) {
                vhInfo.bt_send.setVisibility(View.VISIBLE);
                vhInfo.bt_send.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.send(groupPosition);
                    }
                });
            } else {
                vhInfo.bt_send.setVisibility(View.GONE);
            }
        } else {
            vhInfo.ll_func.setVisibility(View.GONE);
            vhInfo.ll_child.setVisibility(View.VISIBLE);
            childPosition--;
            BetterPlanBean bean = datas.get(groupPosition);
            BetterPlanBean.BetterPlanDetailBean item = bean.getOrderDetailList().get(childPosition);

            if (childPosition == 0) {
                vhInfo.view_line1.setVisibility(View.INVISIBLE);
                vhInfo.view_line2.setVisibility(View.VISIBLE);
            } else if (childPosition == bean.getOrderDetailList().size() - 1) {
                vhInfo.view_line1.setVisibility(View.VISIBLE);
                vhInfo.view_line2.setVisibility(View.INVISIBLE);
            } else {
                vhInfo.view_line1.setVisibility(View.VISIBLE);
                vhInfo.view_line2.setVisibility(View.VISIBLE);
            }

            vhInfo.view_line1.getLayoutParams().height = ImageUtil.dip2px(10, mContext);
            vhInfo.tv_reminddate.setText(item.getDoOrderDate().split(" ")[0]);

            vhInfo.ll_items.removeAllViews();
            //开始一行一行的拼数据

            int dp10 = ImageUtil.dip2px(10, mContext);
            for (int big = 0; big < item.getRemindList().size(); big++) {
                BetterPlanBean.BetterPlanDetailRemindBean remindBean = item.getRemindList().get(big);
                LinearLayout ll_itemss = new LinearLayout(mContext);//塞提醒项
                ll_itemss.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                ll_itemss.setOrientation(LinearLayout.VERTICAL);
                ll_itemss.setPadding(dp10, dp10, dp10, dp10);

                View view_divider = new View(mContext);//提醒项和时间直接的分割线
                view_divider.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 2));
                view_divider.setBackgroundColor(ContextCompat.getColor(mContext, R.color.grey_text2));

                LinearLayout ll_times1 = new LinearLayout(mContext);//第一行时间
                ll_times1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                ll_times1.setOrientation(LinearLayout.HORIZONTAL);
                ll_times1.setPadding(dp10, dp10, dp10, dp10);

                LinearLayout ll_times2 = new LinearLayout(mContext);//第2行时间
                ll_times2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                ll_times2.setOrientation(LinearLayout.HORIZONTAL);
                ll_times2.setPadding(dp10, 0, dp10, dp10);
                ll_times2.setVisibility(View.GONE);

                for (BetterPlanBean.BetterPlanDetailRemindItemBean f : remindBean.getItemList()) {
                    TextView tv_content = new TextView(mContext);
                    LinearLayout.LayoutParams lp_tv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    lp_tv.bottomMargin = ImageUtil.dip2px(5, mContext);
                    tv_content.setLayoutParams(lp_tv);

                    tv_content.setTextSize(16);
                    tv_content.setText(f.getContent());
                    if (!f.getType().equals("2")) {
                        tv_content.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                        tv_content.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
                    } else {
                        tv_content.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                    }
                    if (f.getType().equals("1")) {
                        tv_content.setTag(f.getItemId() + "aaaaa" + f.getKindName());
                        tv_content.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                listener.article((String) v.getTag());
                            }
                        });
                    } else {
                        tv_content.setOnClickListener(null);
                    }
                    ll_itemss.addView(tv_content);
                }

                for (int i = 0; i < Math.min(4, remindBean.getTimeIds().size()); i++) {
                    String time = remindBean.getTimeIds().get(i).getTime();
                    TextView tv_time = new TextView(mContext);
                    LinearLayout.LayoutParams lp_time = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    lp_time.rightMargin = ImageUtil.dip2px(5, mContext);
                    tv_time.setLayoutParams(lp_time);
                    tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                    tv_time.setTextSize(14);
                    tv_time.setBackgroundResource(R.drawable.foundation_grey_halfcircle);
                    tv_time.setPadding(ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext), ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext));
                    tv_time.setText(time);
                    ll_times1.addView(tv_time);
                }
                if (remindBean.getTimeIds().size() > 4) {
                    ll_times2.setVisibility(View.VISIBLE);
                    for (int i = 4; i < remindBean.getTimeIds().size(); i++) {
                        String time = remindBean.getTimeIds().get(i).getTime();
                        TextView tv_time = new TextView(mContext);
                        LinearLayout.LayoutParams lp_time = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp_time.rightMargin = ImageUtil.dip2px(5, mContext);
                        tv_time.setLayoutParams(lp_time);
                        tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
                        tv_time.setTextSize(14);
                        tv_time.setBackgroundResource(R.drawable.foundation_grey_halfcircle);
                        tv_time.setPadding(ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext), ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext));
                        tv_time.setText(time);
                        ll_times2.addView(tv_time);
                    }
                } else {
                    ll_times2.setVisibility(View.GONE);
                }

                vhInfo.ll_items.addView(ll_itemss);
                vhInfo.ll_items.addView(view_divider);
                vhInfo.ll_items.addView(ll_times1);
                if (remindBean.getTimeIds().size() > 4) {
                    vhInfo.ll_items.addView(ll_times2);
                }
                if (big < item.getRemindList().size() - 1) {
                    View view_divider2 = new View(mContext);//底部的分割线
                    view_divider2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 2));
                    view_divider2.setBackgroundColor(ContextCompat.getColor(mContext, R.color.grey_text2));
                    vhInfo.ll_items.addView(view_divider2);
                }
            }
        }
        return convertView;
    }

    private class viewholderTitle {
        TextView tv_title;
        ImageView iv_arrow;
    }

    private class viewholderInfo {
        LinearLayout ll_func;
        Button bt_delete;
        Button bt_modify;
        Button bt_send;
        LinearLayout ll_child;
        View view_line1;
        View view_line2;
        TextView tv_reminddate;
        LinearLayout ll_items;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return childId;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return groupId;
    }

    public interface onBetterplanFunctionListener {
        void send(int pIndex);//发送，助手可以

        void article(String id);//文章id和所属类型

        void delete(int pIndex);//删除order

        void modify(int pIndex);//修改order
    }
}
